/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.phone.service.impl;

import me.zhengjie.phone.domain.SmsPhone;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.phone.service.SmsPhoneService;
import me.zhengjie.phone.domain.vo.SmsPhoneQueryCriteria;
import me.zhengjie.phone.mapper.SmsPhoneMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author nobody
* @date 2025-01-08
**/
@Service
@RequiredArgsConstructor
public class SmsPhoneServiceImpl extends ServiceImpl<SmsPhoneMapper, SmsPhone> implements SmsPhoneService {

    private final SmsPhoneMapper smsPhoneMapper;

    @Override
    public PageResult<SmsPhone> queryAll(SmsPhoneQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(smsPhoneMapper.findAll(criteria, page));
    }

    @Override
    public List<SmsPhone> queryAll(SmsPhoneQueryCriteria criteria){
        return smsPhoneMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SmsPhone resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SmsPhone resources) {
        SmsPhone smsPhone = getById(resources.getSmsPhoneId());
        smsPhone.copy(resources);
        saveOrUpdate(smsPhone);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<SmsPhone> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SmsPhone smsPhone : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户token", smsPhone.getSmsUserToken());
            map.put("手机token", smsPhone.getSmsPhoneToken());
            map.put("平台token", smsPhone.getSmsPlatformToken());
            map.put("手机号", smsPhone.getSmsPhoneNumber());
            map.put("手机号平台key", smsPhone.getSmsPhoneKey());
            map.put("手机号提取时间", smsPhone.getSmsPhoneCreateTime());
            map.put("归属国家代码", smsPhone.getSmsPhoneCountryCode());
            map.put("归属区域代码", smsPhone.getSmsPhoneAreaCode());
            map.put("归属地", smsPhone.getSmsPhonePosition());
            map.put("端口号", smsPhone.getSmsPhonePort());
            map.put("对接码", smsPhone.getSmsPhoneAcceptCode());
            map.put("状态码", smsPhone.getSmsPhoneStatus());
            map.put("原始值", smsPhone.getSmsOriginal());
            map.put("更新时间", smsPhone.getSmsPhoneUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}